<template>
	<el-card shadow="never" header="密码修改">
		<el-alert title="密码更新成功后，您将被重定向到登录页面，您可以使用新密码重新登录。" type="info" show-icon style="margin-bottom: 15px;"/>
		<el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin-top:20px;">
			<el-form-item label="当前密码" prop="userPassword">
				<el-input v-model="form.userPassword" type="password" show-password placeholder="请输入当前密码"></el-input>
				<div class="el-form-item-msg">必须提供当前登录用户密码才能进行更改</div>
			</el-form-item>
			<el-form-item label="新密码" prop="newPassword">
				<el-input v-model="form.newPassword" type="password" show-password placeholder="请输入新密码"></el-input>
				<sc-password-strength ref="passwordStrength" v-model="form.newPassword"></sc-password-strength>
				<div class="el-form-item-msg">请输入包含字母、数字的8位以上密码</div>
			</el-form-item>
			<el-form-item label="确认新密码" prop="confirmNewPassword">
				<el-input v-model="form.confirmNewPassword" type="password" show-password placeholder="请再次输入新密码"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="save">保存密码</el-button>
				<el-button @click="passwordGeneratorVisible = true">生成密码</el-button> 
			</el-form-item>
		</el-form>
	</el-card>
	<password-generator v-if="passwordGeneratorVisible" @success="handleGenPassword" @closed="passwordGeneratorVisible=false"></password-generator>
</template>

<script>
	import scPasswordStrength from '@/components/scPasswordStrength8'
	import PasswordGenerator from '@/components/ncPasswordGenerator'
	import setting from '@/views/sys/user/setting'
	import JSEncrypt from 'jsencrypt'
	export default {
		components: {
			scPasswordStrength,
			PasswordGenerator
		},
		data() {
			return {
				passwordGeneratorVisible: false,
				form: {
					userPassword: "",
					newPassword: "",
					confirmNewPassword: ""
				},
				rules: {
					userPassword: [
						{ required: true, message: '请输入当前密码'}
					],
					newPassword: [
						{ required: true, message: '请输入新密码'},
						{validator: (rule, value, callback) => {
							this.$refs.passwordStrength.strength(value)
							let passLevel = this.$refs.passwordStrength.level
							if (passLevel < 2) {
								callback(new Error('密码复杂度不足'));
							}else{
								callback();
							}
						}}
					],
					confirmNewPassword: [
						{ required: true, message: '请再次输入新密码'},
						{validator: (rule, value, callback) => {
							if (value !== this.form.newPassword) {
								callback(new Error('两次输入密码不一致'));
							}else{
								callback();
							}
						}}
					]
				},
				publicKey: null
			}
		},
		async mounted () {
			//导入 public key
			let pubk = await this.$API.system.pub.publicKey.get();
			this.publicKey = (pubk.code == 0) ? pubk.data : null;
		},
		methods: {
			save(){
				this.$refs.form.validate(async valid => {
					if (valid) {
						let d = {
							id : this.form.id,
							oldPassword: '',
							password: ''
						}

						//
						if (this.publicKey) {							
							let encrypt = new JSEncrypt();
							encrypt.setPublicKey(this.publicKey)
							d.oldPassword = encrypt.encrypt(this.form.userPassword)
							d.password = encrypt.encrypt(this.form.newPassword)
						} else {
							d.oldPassword = this.form.userPassword
							d.password = this.form.newPassword
						}

						//
						var res = await setting.api.passwordModify.post(d)
						if(res.code == setting.successCode){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("重置成功,即将跳转到登录页");
							
							var tt = this;
							setTimeout(async function(){
								//登出
								await tt.logout()
								tt.$router.replace({
									path: '/login'
								})
							},3000);

						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false
					}
				})
			},
			async logout(){
				await this.$API.auth.logout.post();
			},
			handleGenPassword(p) {
				if (p) {
					this.form.newPassword = p
					this.form.confirmNewPassword = p
				}
			}
		}
	}
</script>

<style>
</style>
